<?php

namespace System\Loader;

/**
 * 装载器抽象类
 *
 * @package Dream
 * @subpackage LoaderAbstract
 * @namespace System\Loader
 * @author lewis (lewisld@yeah.net)
 */
abstract class LoaderAbstract extends LoaderObject
{

    /**
     * 注册的命名空间列表
     *
     * @var array
     */
    protected $nameSpace = [];

    /**
	 * 注册命名空间
	 *
	 * @static
	 * @access public
	 * @param string $name 命名空间
	 * @param string $libraryPath 命名空间类库路径
	 * @return void 没有返回值
	 */
    public function registerNameSpace($name, $libraryPath)
    {
        if (is_array($name)) {
            foreach ($name as $key => $value) {
                if (!empty($value)) {
                    if ($key[0] == '\\') {
                        $key = substr($key, 1);
                    }
                    $key = preg_replace('/\\+/', '\\', $key);
                    $this->nameSpace[$key] = (string) Alias::getPath($value);
                }
            }
            unset($key, $value);
        } else {
            if (!empty($libraryPath)) {
                if ($name[0] == '\\') {
                    $name = substr($name, 1);
                }
                $name = preg_replace('/\\+/', '\\', $name);
                $this->nameSpace[$name] = (string) Alias::getPath($libraryPath);
            }
            unset($name);
        }
    }

    /**
	 * 卸载命名空间
	 *
	 * @static
	 * @access public
	 * @param string $name 命名空间
	 * @param string $libraryPath 命名空间类库路径
	 * @return void 没有返回值
	 */
    public function unregisterNameSpace($name)
    {
        if(isset($this->nameSpace[$name])){
            unset($this->nameSpace[$name]);
        }
    }

    abstract public function loadClass($class);

    abstract protected function findClass($class);

}
